package com.rrd.dw.mr.gzip.bhzxzd;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer.Context;

import com.rrd.dw.utils.DateUtil;

public class OverdueProcess {
	public static final String CRT_FLAG = "\001";
    public static final Pattern CRT_PATTERN = Pattern.compile(CRT_FLAG);
    private static NullWritable key_null = NullWritable.get();
	private static Text val_Text = new Text();
	private final static int K=10;
	public static String process_overdue_pay(Context context,List<Map<String,String>> list_maps,Map<String,String> map,Map<String,List<String>> pay_map,String min_due_date) throws IOException, InterruptedException{
		String target_repayment_date="";
		String next_target_repayment_date_k="";
		target_repayment_date=BHZXZDUtils.time_to_date(map.get("target_repayment_date"));
		String target_repayment_date_k=DateUtil.formateDateStr(DateUtil.addDay(DateUtil.formateToDate(target_repayment_date,"yyyy-MM-dd"), K+1),"yyyy-MM-dd") ;
		if(!map.get("next_due_date").equals("")){
			next_target_repayment_date_k=DateUtil.formateDateStr(DateUtil.addDay(DateUtil.formateToDate(map.get("next_due_date"),"yyyy-MM-dd"), K+1),"yyyy-MM-dd") ;
		} 
		int old_days=0;
		 List<String> list = new ArrayList<String>(pay_map.keySet());
		 Collections.sort(list); 
		for(String payoff_date:list){
			if(payoff_date.compareTo(target_repayment_date_k)>=0 && (next_target_repayment_date_k.equals("") || payoff_date.compareTo(next_target_repayment_date_k)<0  )){
				double  due_amt=0;
				double  rmain_amt=0;
				double act_amt=0;
				double overdue_amount=0;
				List<String> termList= pay_map.get(payoff_date);
				StringBuilder sb = new StringBuilder();
				min_due_date="";
				for(Map<String,String> mm:list_maps){
					if(Integer.parseInt(mm.get("term_no"))<=Integer.parseInt(map.get("term_no")) && !mm.get("is_true").equals("true")){
//						if(mm.get("period_status").equals("ADVANCE_PAYOFF")){
//							due_amt=due_amt+Double.parseDouble(mm.get("planned_payment"));
//						}else{
//							due_amt=due_amt+Double.parseDouble(mm.get("planned_payment"))-Double.parseDouble(mm.get("deduction_amount"));
//						}
						due_amt=due_amt+Double.parseDouble(mm.get("planned_payment"))-Double.parseDouble(mm.get("deduction_amount"));
					}
					if(termList.contains(mm.get("term_no"))){
						mm.put("is_true", "true"); 
						act_amt=act_amt+Double.parseDouble(mm.get("planned_payment"));
						sb.append(mm.get("term_no")).append(",");
					}
					if(!mm.get("is_true").equals("true")){
						rmain_amt=rmain_amt+Double.parseDouble(mm.get("planned_principal"));
					}
					if(Integer.parseInt(mm.get("term_no"))<=Integer.parseInt(map.get("term_no")) && !mm.get("is_true").equals("true")){
						overdue_amount=overdue_amount+Double.parseDouble(mm.get("planned_payment"));
					} 
					if( !mm.get("is_true").equals("true")){
						if(min_due_date.equals("") || mm.get("target_repayment_date").compareTo(min_due_date)<=0){
							min_due_date=mm.get("target_repayment_date");
						}
					}
				}
				if(overdue_amount>0){
					old_days=DateUtil.date_diff(payoff_date, min_due_date);
				}else{
					old_days=0;
					min_due_date="";
				}
				map.put("target_repayment", String.valueOf(due_amt));
				map.put("remaining_amount", String.valueOf(rmain_amt));
//				lastRemainAmt = rmain_amt;
				map.put("real_repayment", String.valueOf(act_amt));
				map.put("overdue_amount", String.valueOf(overdue_amount)); //当 前 逾期总额 
//				if(map.get("period_status").equals("ADVANCE_PAYOFF")){
//					map.put("term_status",old_days<=30?"thirdPartyPayIn30":"compensation");	 
//				}else{ 
//					map.put("term_status",old_days>K?"overdue":"normal");
//				} 
				map.put("term_status",old_days>K?"overdue":"normal");
				map.put("real_repayment_date",payoff_date); //实际还款时间
				map.put("overdue_status",old_days>K?"D"+old_days:"");
				map.put("status_confirm_at",payoff_date);
				if(map.get("last_due_date").equals(map.get("target_repayment_date")) &&  old_days<=0){
					
					if(map.get("period_status").equals("ADVANCE_PAYOFF")){
						map.put("loan_status", "5");  
					}else{ 
						map.put("loan_status", "3"); 
					} 
					
				}else if(old_days>K){
					map.put("loan_status", "2");
				}else{
					map.put("loan_status", "1");
				} 
				map.put("period_list", sb.toString().equals("")?"":sb.substring(0,sb.length()-1));	
				val_Text.set(BHZXZDUtils.processZhudaiToString(map));
				context.write(key_null, val_Text);
				
			}
			
		}	
			 
		return min_due_date;
		
			
}
	public static void process_last_period(Context context,List<Map<String,String>> list_maps,Map<String,String> map,String min_due_date,String dt) throws IOException, InterruptedException{
		String confirm_date=DateUtil.formateDateStr(DateUtil.addDay(DateUtil.formateToDate(map.get("target_repayment_date"),"yyyy-MM-dd"), 30),"yyyy-MM-dd");
		String pre_confirm_date=""; 
	    int old_days=0; 
	    String[] arr= getLoanLastAmount(list_maps);
	    String payoff_date ="";
	    if(!payoff_date.equals("")){
	    	payoff_date=BHZXZDUtils.time_to_date(map.get("payoff_time")); 
	    }
	    map.put("real_repayment_date","");
		while(confirm_date.compareTo(dt)<0 && (payoff_date.equals("") || confirm_date.compareTo(payoff_date)<0)){
			pre_confirm_date=confirm_date;
			confirm_date=DateUtil.formateDateStr(DateUtil.addDay(DateUtil.formateToDate(confirm_date,"yyyy-MM-dd"), 30),"yyyy-MM-dd");
			old_days=DateUtil.date_diff(confirm_date, min_due_date);
			map.put("target_repayment", arr[0]);
			map.put("remaining_amount", arr[1]);
			map.put("real_repayment", String.valueOf(0));
			map.put("overdue_amount", arr[0]); //当 前 逾期总额 
			map.put("term_status","overdue");
			map.put("overdue_status","D"+old_days);
			map.put("status_confirm_at",confirm_date);
			map.put("loan_status", "2");  
			map.put("period_list","");
			val_Text.set(BHZXZDUtils.processZhudaiToString(map));
			context.write(key_null, val_Text);
			confirm_date=DateUtil.formateDateStr(DateUtil.addDay(DateUtil.formateToDate(pre_confirm_date,"yyyy-MM-dd"), 30),"yyyy-MM-dd");
		}
	}	
	/**
	 * 
	 * @param list_maps
	 * @return
	 */
	public static String[] getLoanLastAmount(List<Map<String,String>> list_maps){
		double loan_due_amt=0; 
		double remain_amt=0;
		String[] arr=new String[2]; 
		for(Map<String,String> m :list_maps){
			if(!m.get("is_true").equals("true")){
//				if(m.get("period_status").equals("ADVANCE_PAYOFF")){
//					loan_due_amt=loan_due_amt+Double.parseDouble(m.get("planned_payment"));
//				}else{
//					loan_due_amt=loan_due_amt+Double.parseDouble(m.get("planned_payment"))-Double.parseDouble(m.get("deduction_amount"));
//				}
				loan_due_amt=loan_due_amt+Double.parseDouble(m.get("planned_payment"))-Double.parseDouble(m.get("deduction_amount"));
				remain_amt=remain_amt+Double.parseDouble(m.get("planned_principal"));
			}
		}
		arr[0]=String.valueOf(loan_due_amt);
		arr[1]=String.valueOf(remain_amt);
		
		return arr;
	} 
	public static void main(String[] args) {
		Map<String,String> pay_map = new HashMap<String,String>();
		pay_map.put("2018-01-23", "ss");
		pay_map.put("2017-02-23", "ss");
		pay_map.put("2018-08-23", "ss");
		pay_map.put("2018-09-23", "ss");
		pay_map.put("2016-12-23", "ss");
		pay_map.put("2018-01-23", "ss");
		
		 List<String> list = new ArrayList<String>(pay_map.keySet());
		 Collections.sort(list); 
		 for(String s:list){
			 System.out.println(s);
		 }
	}
}
